<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.regular.recommend.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('business.regular.recommend.start_harbor_code')" prop="start_harbor_code">
            <el-select v-model="dataForm.start_harbor_code" :placeholder="i18n('common.please_select') + i18n('business.regular.recommend.start_harbor_code')">
              <el-option v-for="(v, k) in routeList" :label="v.harbor_name" :key="k" :value="v.harbor_code">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('business.regular.recommend.end_harbor_code')" prop="end_harbor_code">
            <el-select v-model="dataForm.end_harbor_code" :placeholder="i18n('common.please_select') + i18n('business.regular.recommend.end_harbor_code')">
              <el-option v-for="(v, k) in routeList" :label="v.harbor_name" :key="k" :value="v.harbor_code">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('common.is_recommend')" prop="is_recommend">
            <el-switch
              v-model="dataForm.is_recommend"
              :active-value="1"
              :inactive-value="2"
              inline-prompt
              :active-text="$t('common.recommended')"
              :inactive-text="$t('common.unrecommended')"
              style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949">
            </el-switch>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('module:business:regular:recommend:handle')" type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-regular-recommend-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/regular/recommend')

  const routeList = ref([])

  const dataForm = reactive({
    id: 0,
    information_id: '',
    start_harbor_code: '',
    end_harbor_code: '',
    is_recommend: '2',
  })

  const dataRule = reactive({
    is_recommend: [{
      required: true,
      message: i18n('advertising.rules.is_recommend.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.information_id = query.information_id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.information_id) {
        $http({
          url: $http.adornUrl(`/business/regular/view/${dataForm.information_id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.information_id = data.data.information_id
            dataForm.start_harbor_code = data.data.start_harbor_code
            dataForm.end_harbor_code = data.data.end_harbor_code
            dataForm.is_recommend = data.data.is_recommend
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            sss: 1,
            information_id: dataForm.information_id,
            start_harbor_code: dataForm.start_harbor_code,
            end_harbor_code: dataForm.end_harbor_code,
            is_recommend: dataForm.is_recommend
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadRouteList = () => {
    $http({
      url: $http.adornUrl('/business/regular/route/select'),
      method: 'get',
      params: $http.adornParams({
        information_id: dataForm.information_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        routeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadRouteList()
  })
</script>
